<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="序列名称" prop="name">
                <el-input
                        v-model="queryParams.name"
                        placeholder="请输入序列名称"
                        clearable
                        size="small"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="使用表" prop="userTable">
                <el-input
                        v-model="queryParams.userTable"
                        placeholder="请输入使用表"
                        clearable
                        size="small"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button
                        type="success"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增
                </el-button>
                <el-button
                        type="warning"
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                >导出
                </el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" border :data="sequenceList">
            <el-table-column label="序列名称" align="center" prop="name" :show-overflow-tooltip="true"/>
            <el-table-column label="使用表" align="center" prop="userTable" :show-overflow-tooltip="true"/>
            <el-table-column label="序列前缀" align="center" prop="prefixString"/>
            <el-table-column label="字符长度" align="center" prop="strLength"/>
            <el-table-column label="自增步长" align="center" prop="increment"/>
            <el-table-column label="序列当前值" align="center" prop="currentValueStr"/>
            <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
            <el-table-column label="操作时间" align="center" prop="updateTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.updateTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改序列配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="序列名称"  prop="name">
                    <el-input v-model="form.name" :readonly="readonly" placeholder="请输入序列名称，英文"/>
                </el-form-item>
                <el-form-item label="使用表" prop="userTable">
                    <el-input v-model="form.userTable" :readonly="readonly" placeholder="请输入序列使用表"/>
                </el-form-item>
                <el-form-item label="序列前缀" prop="prefixString">
                    <el-input v-model="form.prefixString" placeholder="请输入序列前缀字符"/>
                </el-form-item>
                <el-form-item label="字符长度" prop="strLength">
                    <el-input-number v-model="form.strLength" controls-position="right" :min="8"/>
                </el-form-item>
                <el-form-item label="序列初值" prop="currentValue">
                    <el-input-number v-model="form.currentValue" controls-position="right" :min="1"/>
                </el-form-item>
                <el-form-item label="自增步长" prop="increment">
                    <el-input-number v-model="form.increment" controls-position="right" :min="1"/>
                </el-form-item>
                <el-form-item label="用途说明" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入用途说明"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {addSequence,listSequence,modifySequence,exportSequence} from "@/api/system/sequence";

    export default {
        name: "Dict",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 总条数
                total: 0,
                // 字典表格数据
                sequenceList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    name: undefined,
                    userTable: undefined
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    name: [
                        {required: true, message: "序列名称不能为空", trigger: "blur"}
                    ],
                    userTable: [
                        {required: true, message: "序列使用表不能为空", trigger: "blur"}
                    ]
                },
                readonly: false
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询字典类型列表 */
            getList() {
                this.loading = true;
                listSequence(this.queryParams).then(response => {
                        this.sequenceList = response.rows;
                        this.total = response.total;
                        this.loading = false;
                    }
                );
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    sequenceId: undefined,
                    name: undefined,
                    userTable: undefined,
                    prefixString: undefined,
                    currentValue: 1,
                    strLength: 8,
                    increment: 1,
                    remark: undefined
                };
                this.resetForm("form");
            },
            /** 查询按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.readonly = false;
                this.title = "新增序列";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.form = row;
                this.open = true;
                this.readonly = true;
                this.title = "修改序列信息";
            },
            /** 提交按钮 */
            submitForm: function () {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.sequenceId != undefined) {
                            modifySequence(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                } else {
                                    this.msgError(response.msg);
                                }
                            });
                        } else {
                            addSequence(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                } else {
                                    this.msgError(response.msg);
                                }
                            });
                        }
                    }
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出当前序列信息?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return exportSequence(queryParams);
                }).then(response => {
                    this.download(response.msg);
                }).catch(function () {
                });
            }
        }
    };
</script>
